<?php
header('Content-type:text/html;charset=utf-8');
//在被包含页面不要再包含文件
//包含数据库配置文件
include '../public/dbconfig.php';

//包含数据库文件
include '../public/func.inc.php';

session_start();
//接受用户信息  验证 存储到用户表中
//var_dump($_POST);
//exit;
$uname = $_POST['uname'];
$pass = $_POST['password'];
$repass = $_POST['repassword'];
$email = $_POST['email'];
$yzm = $_POST['yzm'];
$code = $_SESSION['code'];


//验证码验证

if($yzm != $code){
	echo '<script>alert("验证码不符,请重新输入");window.location.href="add.html";</script>';
}

//验证  正则 字符串相关函数
//1 用户名长度不能小于6
$len = strlen($uname);
if($len < 6){
	echo '<script>alert("用户名长度不能小于6");window.location.href="add.html";</script>';
	exit;
}


if($pass != $repass){
	echo '<script>alert("密码与确认密码不一致");window.location.href="add.html";</script>';
	exit;
	
}else{
	$newpass = md5($pass);
	
}

//获取注册时间
$rtime = time();

//获取注册IP
$rip = $_SERVER['REMOTE_ADDR'];
//var_dump($rip);
//exit;


if($rip == '::1'){
	$rip = '127.0.0.1';
}
//数据表中的ip为int
$longip =ip2long($rip);
//var_dump($longip);
//exit;
//连接数据库
$link = conn();
//var_dump($link);

//准备添加用户sql 语句
$sql = "insert into user(username,password,rtime,rip,email) values('{$uname}','{$newpass}','{$rtime}','{$longip}','{$email}')";
//var_dump($sql);
//exit;
//echo $sql;

//执行添加用户操作
$res = mysqli_query($link,$sql);

//var_dump($res);
//exit;
if($res){
	echo  '<script>alert("恭喜,添加成功");window.location.href="index.php";</script>';
}else{
	echo '<script>alert("添加失败");window.location.href="add.html";</script>';
}

//关闭数据库连接

mysqli_close($link);